package com.whx.controller;

import com.whx.pojo.Article;
import com.whx.pojo.PageBean;
import com.whx.pojo.Result;
import com.whx.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping
    public Result add(@RequestBody @Validated(Article.Add.class) Article article) {
        articleService.add(article);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Article>> list(Integer pageNum, Integer pageSize, @RequestParam(required = false) Integer categoryId, @RequestParam(required = false) String state) {
        PageBean<Article> ls = articleService.list(pageNum, pageSize, categoryId, state);
        return Result.success(ls);
    }

    @GetMapping("/detail")
    // 该接口用于根据ID获取文章详细信息
    public Result<Article> detail(Integer id) {
        Article at = articleService.detail(id);
        return Result.success(at);
    }

    @PutMapping
    // 该接口用于更新文章信息
    public Result update(@RequestBody @Validated(Article.Update.class) Article article) {
        articleService.update(article);
        return Result.success();
    }

    @DeleteMapping
    // 该接口用于根据ID删除文章
    public Result delete(Integer id) {
        articleService.delete(id);
        return Result.success();
    }


}
